%------------------------------------------------------------------
%------------------------------------------------------------------
%   Author: Md. Sazzad Hussain (sazzad.hussain@sydney.edu.au)
%   Learning and Affect Technologies Engineering (LATTE)
%   University of Sydney, 2011
%------------------------------------------------------------------
%------------------------------------------------------------------

function [x] = featAnalysis(indir,outdir,ch,win,fName,opSys)
%anlaysis on features selection in feature selection process

if opSys==1;
    sep='/';
elseif opSys==2
    sep='\';
end

mkdir(outdir)
fid=fopen([outdir, sep, fName '_featAnalysis.txt'],'w');

fileDir=dir(indir);
for i=1:length(fileDir)
    [path,name,ext,ver] = fileparts(fileDir(i).name);
    if strcmp(ext,'.txt')
        loadFiles=[indir sep name ext];%files full path
        fprintf(fid,'%s\n\n',loadFiles); %print fileName
        
        C=textread(loadFiles,'%s','delimiter','\n');%read files
        
        featListS=sort(C);%sorted all lines in list
        totalFeatAll=length(featListS);%total num of lines in list
        
        featListF=fliplr(char(featListS));
        featListF=strtrim(cellstr(featListF));%flip sorted list
        
        winListF=fliplr(char(win));
        winListF=strtrim(cellstr(winListF));%flip winSize label
        
        if size(win,1)>0
            fprintf(fid,'%s\n','Window Analysis'); %print header
            fprintf(fid,'%s\n','---------------'); %print to file
            sumAllWin=0;
            for j=1:length(win)   %window analysis
                n1=length(char(win(j)));
                sumWin=sum(strncmp(featListF,winListF(j),n1));
                sumAllWin=sumAllWin+sumWin;%total num of features
            end
                        
            for j=1:length(win)   %window analysis
                n1=length(char(win(j)));
                sumWin=sum(strncmp(featListF,winListF(j),n1));
                winPrint=strcat(strcat(win(j), ' : '),num2str((sumWin/sumAllWin)));
                fprintf(fid,'%s\n',char(winPrint)); %print to file
            end
            
            fprintf(fid,'%s\n','xxxxxxxxxxxxxxx'); %print to file
            fprintf(fid,'%s\n',''); %print to file
            
            fprintf(fid,'%s\n','ChWindow Analysis'); %print header
            fprintf(fid,'%s\n','---------------'); %print to file
            for j=1:length(ch)%channel
                sumCh=0;
                n1=length(char(ch(j)));
                sumCh=sum(strncmp(featListS,ch(j),n1));
                for k=1:length(win)%window
                    sumAllCh=0;
                    n2=length(char(win(k)));
                    chANDwin=strncmp(featListF,winListF(k),n2) & strncmp(featListS,ch(j),n1);% (ch and win)
                    sumAllCh=sum(chANDwin);
                    chWinPrint=strcat(strcat(strcat(ch(j),win(k)), ' : '),num2str(sumAllCh/sumCh));
                    fprintf(fid,'%s\n',char(chWinPrint)); %print to file
%                     sumCh=sumCh+sumAllCh;
                end
                fprintf(fid,'%s\n',strcat('Total :',num2str(sumCh))); %print to file
                fprintf(fid,'%s\n',strcat('Percentage (Ch/all) :',num2str(sumCh/sumAllWin))); %print to file
                fprintf(fid,'%s\n','---------------'); %print to file
            end
            
        elseif size(win,1)==0
            featListU=unique(featListS); %unique feat list with winSize
            t=1;
            featList={};
            for k=1:length(featListU)
                tempSplit=regexp(featListU{k},'_','split');%split colums
                p=size(tempSplit,2);
                
                if (p==2)
                    featList(t,:)=tempSplit;
                    t=t+1;
%                 elseif (p>2)
%                     featList(k,:)=[strcat(tempSplit(1),strcat('-',tempSplit(2))), tempSplit(end)];
                end
            end
            featList=featList(:,1);
            featList=unique(featList); %unique feat list without winSize
            for l=1:length(featList)
                fprintf(fid,'%s\n',char(featList(l))); %print to file  
            end
        end
    end
    fid(close);
    x=1;
end

